// var serviceUrl='http://172.16.31.162:8868';
// var serviceUrl='http://120.79.241.160:8868';

var serviceUrl='http://bi-test.sqqmall.com';

// 激活图
var myChart = echarts.init(document.getElementById('main'));
var myChart1 = echarts.init(document.getElementById('main1'));
// 指定图表的配置项和数据
var option = {
    color:[],
    tooltip : {
        trigger: 'axis',
        axisPointer: {
            type: 'cross',
            label: {
                backgroundColor: '#6a7985'
            }
        }
    },
    legend: {
        data:[],
        left: '2%',
        top: '4%'
    },
    grid: {
        left: '2%',
        right: '3%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: {
        type: 'category',
        data: []
    },
    yAxis: {
        type: 'value'
    },
    series: []
};
// 普通卡图形
var myChartpie1 = echarts.init(document.getElementById('pie1'));
var peioption1={
    tooltip: {
        trigger: 'item',
        position: [0,0],
        formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    color:['#FF9945','#4CDFC2','#5EB0EF','#F9CF2B'],
    series: [
        {
            name:'生成卡的数量',
            type:'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
                normal: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    show: true,
                    textStyle: {
                        fontSize: '16',
                        fontWeight: '500',
                        shadowOffsetY: '0'
                    }
                }
            },
            labelLine: {
                normal: {
                    show: true
                }
            },
            data:[]
        }
    ]
};
// 定制卡
var myChartpie2 = echarts.init(document.getElementById('pie2'));
var peioption2={
    tooltip: {
        trigger: 'item',
        position: [0,0],
        formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    color:['#F3f3f3','#4CDFC2','#5EB0EF','#F9CF2B'],
    series: [
        {
            name:'生成卡的数量',
            type:'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
                normal: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    show: true,
                    textStyle: {
                        fontSize: '16',
                        fontWeight: '500',
                        shadowOffsetY: '0'
                    }
                }
            },
            labelLine: {
                normal: {
                    show: true
                }
            },
            data:[]
        }
    ]
};

//各平台优惠券兑换情况分析
var myChartpie3 = echarts.init(document.getElementById('pie3'));
var peioption3={
    tooltip: {
        trigger: 'item',
        position: [0,0],
        formatter: "{a} <br/>{b}: {c} ({d}%)"
    },
    color:['#55ABEF','#4AE0C2','#FF9946','#F9CF2B','#788BF1','#2FC993','#FEC580','#5F74F3','#F09489','#42496D'],
    series: [
        {
            name:'优惠券兑换情况',
            type:'pie',
            radius: ['35%', '50%'],
            avoidLabelOverlap: false,
            label: {
                normal: {
                    formatter: '{b|{b}  {d}%}',
                    rich: {
                        b: {
                            fontSize: 12,
                            lineHeight: 310
                        },
                        per: {
                            color: '#eee',
                            backgroundColor: '#334455',
                            padding: [2, 4],
                            borderRadius: 2
                        }
                    }
                },
                emphasis: {
                    show: true,
                    textStyle: {
                        fontSize: '16',
                        fontWeight: '500',
                        shadowOffsetY: '0'
                    }
                }
            },
            labelLine: {
                normal: {
                    show: true
                }
            },
            data:[],
            itemStyle: {
                emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }
    ]
};
//优惠券兑换频次人数分布情况分析
var myChartzhu4 = echarts.init(document.getElementById('zhu4'));
var zhuoption4={
    color: ['#00D0D0'],
    tooltip : {
        trigger: 'axis',
        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
        }
    },
    grid: {
        left: '2%',
        right: '2%',
        bottom: '3%',
        containLabel: true
    },
    xAxis : [
        {
            type : 'category',
            data : ['0次','1次','2次','3-6次','7-12次','13-24次','26-36次','37次以上'],
            axisTick: {
                alignWithLabel: true
            }
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name:'兑换人数',
            type:'bar',
            barWidth: '25%',
            label: {
                normal: {
                    show: true,
                    position: 'top'
                }
            },
            data:[]
        }
    ]
};




//说明弹出/关闭
var model = function (num,type,text) {
    if(num==1){
        document.getElementById('model').style.display="block";
    }else{
        document.getElementById('model').style.display="none";
    }
    if(type=='money'){
        document.getElementById('p1').innerHTML='会员总余额：注册会员已充值到账户的剩余卡余额';
    }else if(type=='login'){
        document.getElementById('p1').innerHTML='用户激活率=注册会员数 / 销售卡数';
    }else if(type=='msg'){
        document.getElementById('p1').innerHTML=text;
    }
}

// 基础概括数据
var basic = function(){  
    // console.log(1);
    $.ajax({  
        url: serviceUrl+'/coupon-bi/bi/basic',
        type: 'get',
        dataType: 'json',
        headers:{"Content-Type":"text/plain;charset=UTF-8"}, 
        success: function (data, status) {
            console.log('/**** 基础概括数据 ****/');
            console.log(data);
            if(data.code!=0){
                model(1,'msg',data.msg);
                return
            }
            document.getElementById('basic1').innerHTML=parseFloat(data.data.exchangeAmount).toLocaleString();
            document.getElementById('basic2').innerHTML=parseFloat(data.data.exchangePer).toLocaleString();
            document.getElementById('basic3').innerHTML=parseFloat(data.data.exchangePerAvg).toLocaleString();
            document.getElementById('basic4').innerHTML=parseFloat(data.data.activeTotal).toLocaleString();
            document.getElementById('basic5').innerHTML=parseFloat(data.data.activeAmount).toLocaleString();
            document.getElementById('basic6').innerHTML=parseFloat(data.data.totalRemaining).toLocaleString();
            document.getElementById('basic7').innerHTML=parseFloat(data.data.cardNum).toLocaleString();
            document.getElementById('basic8').innerHTML=parseFloat(data.data.cardAmount).toLocaleString();
            document.getElementById('basic9').innerHTML=parseFloat(data.data.regUser).toLocaleString();
            // document.getElementById('basic10').innerHTML=parseFloat(data.data.regUserRate).toLocaleString();
        },
        fail: function (err, status) {
          console.log(err);
          model(1,'msg',err.msg);
        }
    })
}  
basic();

/* 折现图模块 */
var lastDay={};             //昨日
var last7day={};            //近七天
var last30day={};           //近30天
var lastMonth={};           //月
var activatingType=0;       //时间type
var tabType=0;              //切换type
//激活卡数、激活金额、兑换金额
var activatingCard = function(text){  
    $.ajax({  
        url: serviceUrl+text, 
        dataType: 'json',
        headers:{"Content-Type":"text/plain;charset=UTF-8"}, 
        success: function (data, status) {
            console.log('/**** 激活卡数、激活金额、兑换金额 ****/');
            console.log(data);
            if(data.code!=0){
                model(1,'msg',data.msg);
                return
            }
            lastDay=data.data.lastDay;
            last7day=data.data.last7day;
            last30day=data.data.last30day;
            lastMonth=data.data.lastMonth;

            switch(activatingType){
                case 0:
                    lineChart(0);
                    break;
                case 1:
                    lineChart(1,'近7日',last7day);
                    break;
                case 2:
                    lineChart(2,'近30日',last30day);
                    break;
                case 3:
                    lineChart(3,'月',lastMonth);
                    break;
                default:
                    console.log('其他')
            } 

            // lineChart(activatingType,'近7日',last7day);
        },
        fail: function (err, status) {
          console.log(err);
          model(1,'msg',err.msg);
        }
    })
} 
//激活信息切换
$('.activating-tab-box>div').click(function(){
    var index=$(this).index();
    tabType=index;
    var left=index*56+(2*index+1)*12;
    $('.activating-tab-swiper>div').css('margin-left',left);
    $('.activating-tab-box>div').removeClass('active');
    $(this).addClass('active');
    switch(index){
        case 0:
            activatingCard('/coupon-bi/bi/activeCard');
            break;
        case 1:
            activatingCard('/coupon-bi/bi/activeAmount');
            break;
        case 2:
            activatingCard('/coupon-bi/bi/exchangeAmount');
            break;
        default:
            console.log('其他')
    }
}) 
var option222 = {
    color:['#00D0D0'],
    tooltip : {
        trigger: 'axis',
        axisPointer: {
            type: 'cross',
            label: {
                backgroundColor: '#6a7985'
            }
        }
    },
    legend: {
        data:[],
        left: '2%',
        top: '4%'
    },
    grid: {
        left: '2%',
        right: '3%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: {
        type: 'category',
        data: []
    },
    yAxis: {
        type: 'value'
    },
    series: []
};

var lineChart = function(index,text,dayArr){
    if(index==0){
        $('#main').css('display','block');
        $('#main1').css('display','none');
        option.legend.data=['昨日','上一日'];               //分类
        option.color= ['#00D0D0','#FF9900'] ;              //颜色
        var arrs=[
            {
                name:'昨日',
                type:'line',
                data:[]
            },
            {
                name:'上一日',
                type:'line',
                data:[]
            }
        ];
        var timeArrs=[];
        for(var i=0;i<lastDay.currentRound.length;i++){
            arrs[0].data.push(lastDay.currentRound[i].totalCount);
            timeArrs.push(lastDay.currentRound[i].theTime);
        }
        for(var i=0;i<lastDay.lastRound.length;i++){
            arrs[1].data.push(lastDay.lastRound[i].totalCount);
        }
        // console.log(timeArr);
        var tabTime=`昨日 <text>（${lastDay.dateBeginEnd}）</text>`;
        document.getElementById('timeTabTime').innerHTML=tabTime;
        var extentHtml=`
            <text>${parseFloat(lastDay.activieTotal).toLocaleString()} </text> ${tabType==0?'张':'元'} 
            <text>上一日 ${parseFloat(lastDay.lastActivieTotal).toLocaleString()} </text>
            <text class="${lastDay.activieTotal-lastDay.lastActivieTotal>=0?'greed':'red'}">${lastDay.activieTotal-lastDay.lastActivieTotal>=0?'↑':'↓'} ${lastDay.rat}</text>
        `
        document.getElementById('timeTabExtent').innerHTML=extentHtml;
        option.series=[];
        option.xAxis.data=[];
        var newobj={
            name: '平均值',
            type: 'line',  // 平均值
            data: (function() {
                let sum = []
                for (let i in arrs) {
                    for (let j in arrs[i].data) {
                        if(!sum[j]){
                            sum[j] = 0
                        }
                        sum[j] += arrs[i].data[j]/2
                    }
                }
                return sum
            })(),
            itemStyle: {
                opacity: 0
            },
            lineStyle: {
                opacity: 0
            },
            markLine: {
                data: [
                    {
                        type: 'average',
                        name: '平均数',
                        label: {
                            normal: {
                                position: 'middle',
                                formatter: '{b}: {c}'
                            }
                        }
                    }
                ]
            }
        }
        arrs.push(newobj);
        option.series=arrs;
        option.xAxis.data=timeArrs;
        myChart.setOption(option); 
    }else{
        $('#main').css('display','none');
        $('#main1').css('display','block');
        option222.legend.data=[text];               //分类
        var arr=[
            {
                name:text,
                type:'line',
                data:[]
            }
        ];
        var timeArr=[];
        var Arr=dayArr;
        for(var i=0;i<Arr.currentRound.length;i++){
            arr[0].data.push(Arr.currentRound[i].totalCount);
            timeArr.push(Arr.currentRound[i].theTime);
        }
        // console.log(arr);
        var tabTime=`${text} <text>（${Arr.dateBeginEnd}）</text>`;
        document.getElementById('timeTabTime').innerHTML=tabTime;
        var extentHtml=`
            <text>${parseFloat(Arr.activieTotal).toLocaleString()} </text> ${tabType==0?'张':'元'}
            <text>${text=='近7日'?"上一个7日":(text=='近30日'?"上一个30日":"上月")} ${parseFloat(Arr.lastActivieTotal).toLocaleString()} </text>
            <text class="${Arr.activieTotal-Arr.lastActivieTotal>=0?'greed':'red'}">${Arr.activieTotal-Arr.lastActivieTotal>=0?'↑':'↓'} ${Arr.rat}</text>
        `
        document.getElementById('timeTabExtent').innerHTML=extentHtml;
        var newobj={
            name: '平均值',
            type: 'line',  // 平均值
            data: (function() {
                let sum = []
                for (let i in arr) {
                    for (let j in arr[i].data) {
                        if(!sum[j]){
                            sum[j] = 0
                        }
                        sum[j] += arr[i].data[j]
                    }
                }
                return sum
            })(),
            itemStyle: {
                opacity: 0
            },
            lineStyle: {
                opacity: 0
            },
            markLine: {
                data: [
                    {
                        type: 'average',
                        name: '平均数',
                        label: {
                            normal: {
                                position: 'middle',
                                formatter: '{b}: {c}'
                            }
                        }
                    }
                ]
            }
        }
        arr.push(newobj);
        option222.series=arr;
        option222.xAxis.data=timeArr;
        myChart1.setOption(option222); 
    }
};
activatingCard('/coupon-bi/bi/activeCard');

//激活信息查看时间切换
$('.time-tab-change>div').click(function(){
    var index=$(this).index();
    activatingType=index;
    switch(index){
        case 0:
            lineChart(0);
            break;
        case 1:
            lineChart(1,'近7日',last7day);
            break;
        case 2:
            lineChart(2,'近30日',last30day);
            break;
        case 3:
            lineChart(3,'月',lastMonth);
            break;
        default:
            console.log('其他')
    }    
    // console.log(option)
    $('.time-tab-change>div').removeClass('active');
    $(this).addClass('active');
    // myChart.setOption(option);
})
//定制卡排行榜
var createAmountTop10=[];         //生成金额
var activeAmountTop10=[];         //激活金额
var exchangeAmountTop10=[];       //兑换金额
var createCardTop10=[];           //生成卡数
var activeCardTop10=[];           //激活卡数
var rankTab=0;                    //默认为金额
var rankTablength=3;              //数量记录
var moneyTablength=0;             //金额记录
var cardRankingHtml='';           //HTML
var cardRanking=function(){
    $.ajax({  
        url: serviceUrl+'/coupon-bi/bi/customCardAnalyze',
        type: 'get',
        dataType: 'json',
        headers:{"Content-Type":"text/plain;charset=UTF-8"}, 
        success: function (data, status) {
            console.log('/**** 定制卡排行榜 ****/');
            console.log(data);
            if(data.code!=0){
                model(1,'msg',data.msg);
                return
            }
            createAmountTop10=data.data.createAmountTop10;
            activeAmountTop10=data.data.activeAmountTop10;
            exchangeAmountTop10=data.data.exchangeAmountTop10;
            createCardTop10=data.data.createCardTop10;
            activeCardTop10=data.data.activeCardTop10;
            rankingHtml(createAmountTop10);
        },
        fail: function (err, status) {
          console.log(err);
          model(1,'msg',err.msg);
        }
    })
}
var  rankingHtml = function(arr){
    cardRankingHtml='';
    if(rankTab==1){
        cardRankingHtml=`
            <div>
                <div>排名</div>
                <div class='times'>公司名称</div>   
                <div class="ranking-tab-dz ${rankTablength==3?'active':''}" data-index='3'>生成卡数 <i></i></div>
                <div class="ranking-tab-dz ${rankTablength==4?'active':''}" data-index='4'>激活卡数 <i></i></div>
            </div>
        `;
        $.each(arr,function(key,value){
            // console.log(value);
            cardRankingHtml+=`
                <div>
                    <div>${value.rowNum}</div>
                    <div class='times'>${value.name}</div>
                    <div>${parseFloat(value.totalCount).toLocaleString()}</div>
                    <div>${parseFloat(value.totalRechargeCardno).toLocaleString()}</div>
                </div>
            `;
        })
        document.getElementById('canvasHeadtext').innerHTML='（单位：张）';
    }else{
        var cardRankingHtml=`
            <div>
                <div>排名</div>
                <div>公司名称</div>   
                <div class="ranking-tab-dz ${moneyTablength==0?'active':''}" data-index='0'>生成金额 <i></i></div>
                <div class="ranking-tab-dz ${moneyTablength==1?'active':''}" data-index='1'>激活金额 <i></i></div>
                <div class="ranking-tab-dz ${moneyTablength==2?'active':''}" data-index='2'>兑换金额 <i></i> </div>
            </div>
        `;
        $.each(arr,function(key,value){
            // console.log(value);
            cardRankingHtml+=`
                <div>
                    <div>${value.rowNum}</div>
                    <div>${value.name}</div>
                    <div>${parseFloat(value.companyValue).toLocaleString()}</div>
                    <div>${parseFloat(value.companyActivatedValue).toLocaleString()}</div>
                    <div>${parseFloat(value.comanyRechargeValue).toLocaleString()}</div>
                </div>
            `;
        })
        document.getElementById('canvasHeadtext').innerHTML='（单位：元）';
    }
    document.getElementById('ranking').innerHTML=cardRankingHtml;
}
cardRanking();
//定制卡排行榜
$('.dzcard-tab>div').click(function(){
    // console.log(rankTablength);
    if($(this).index()==0){
        rankTab=0;
        if(moneyTablength==0){
            rankingHtml(createAmountTop10);
        }else if(moneyTablength==1){
            rankingHtml(activeAmountTop10);
        }else if(moneyTablength==2){
            rankingHtml(exchangeAmountTop10);
        }
    }else{
        rankTab=1;
        if(rankTablength==3){
            rankingHtml(createCardTop10);
        }else if(rankTablength==4){
            rankingHtml(activeCardTop10);
        }
    }
    $('.dzcard-tab>div').removeClass('active');
    $(this).addClass('active');
})
//定制卡金额切换
$('#ranking').on('click','.ranking-tab-dz',function(){
    $('.ranking-tab-dz').removeClass('active');
    $(this).addClass('active');
    if($(this).data('index')==0){
        moneyTablength=0;
        rankingHtml(createAmountTop10);
    }else if($(this).data('index')==1){
        moneyTablength=1;
        rankingHtml(activeAmountTop10);
    }else if($(this).data('index')==2){
        moneyTablength=2;
        rankingHtml(exchangeAmountTop10);
    }else if($(this).data('index')<=3){
        rankTablength=3
        rankingHtml(createCardTop10);
    }else if($(this).data('index')==4){
        rankTablength=4
        rankingHtml(activeCardTop10);
    }
})
//生成卡各面值金额占比
function uniq(array){
    var temp = []; //一个新的临时数组
    for(var i = 0; i < array.length; i++){
        if(temp.indexOf(array[i]) == -1){
            temp.push(array[i]);
        }
    }
    return temp;
}
var cardMoneyColorArr=['#55ABEF','#4AE0C2','#FF9946','#F9CF2B','#788BF1','#2FC993','#FEC580','#5F74F3','#F09489','#42496D'];
var cardMoney=function(){
    $.ajax({  
        url: serviceUrl+'/coupon-bi/bi/cardFaceAnalyze',
        type: 'get',
        dataType: 'json',
        headers:{"Content-Type":"text/plain;charset=UTF-8"}, 
        success: function (data, status) {
            console.log('/**** 生成卡各面值金额占比 ****/');
            console.log(data);
            if(data.code!=0){
                model(1,'msg',data.msg);
                return
            }
            let customCardArr=[];            //定制卡数据
            let normalCardArr=[];            //普通卡数据
            var allArr=[];                   //所有卡的面值
            var allColorArr=[];              //所有卡的面值对应颜色
            for(var i=0;i<data.data.customCard.length;i++){
                var obj={value:'',name:''};
                obj.value=data.data.customCard[i].totalCount;
                obj.name=data.data.customCard[i].faceValue+'元';
                if(data.data.customCard.length<=0){
                    obj.value=0;
                    obj.name='暂无';
                }
                customCardArr.push(obj);
                console.log(customCardArr);
                allArr.push(data.data.customCard[i].faceValue+'元');
            }
            if(data.data.customCard.length<=0){
                var obj={value:'',name:''};
                obj.value=0;
                obj.name='暂无';
                customCardArr.push(obj);
            }
            console.log(customCardArr);
            for(var j=0;j<data.data.normalCard.length;j++){
                var obj={value:'',name:''};
                obj.value=data.data.normalCard[j].totalCount;
                obj.name=data.data.normalCard[j].faceValue+'元';
                normalCardArr.push(obj);
                allArr.push(data.data.normalCard[j].faceValue+'元');
            }
            console.log(normalCardArr);
            console.log(allArr);
            allArr=uniq(allArr)    

            console.log(allArr);

            for(var a=0;a<allArr.length;a++){
                allColorArr.push(cardMoneyColorArr[a]);
            }
            console.log(allColorArr);

            //定制卡颜色数组生成
            var customCardColorArr=[];
            for(var c=0;c<customCardArr.length;c++){
                for(var d=0;d<allArr.length;d++){
                    if(customCardArr[c].name==allArr[d]){
                        customCardColorArr.push(allColorArr[d]);
                    }
                }
            }
            console.log(customCardColorArr);
            //普通卡颜色数组生成
            var normalCardColorArr=[];
            for(var c=0;c<normalCardArr.length;c++){
                for(var d=0;d<allArr.length;d++){
                    if(normalCardArr[c].name==allArr[d]){
                        // console.log(allColorArr[d]);
                        normalCardColorArr.push(allColorArr[d]);
                    }
                }
            }
            if(data.data.customCard.length<=0){
                customCardColorArr.push('#f3f3f3');
            }
            console.log(normalCardColorArr);

            peioption1.series[0].data=normalCardArr;
            peioption2.series[0].data=customCardArr;
            peioption1.color=normalCardColorArr;
            peioption2.color=customCardColorArr;
            document.getElementById('customCardSum').innerHTML=parseFloat(data.data.customerTotal).toLocaleString();
            document.getElementById('normalCardSum').innerHTML=parseFloat(data.data.normalTotal).toLocaleString();
            myChartpie1.setOption(peioption1);
            // console.log(peioption2);
            myChartpie2.setOption(peioption2);

            //模块底部颜色生成
            var coloeHtml='';
            // console.log(allArr);
            $.each(allArr,function(key,value){
                // console.log(value);
                coloeHtml+=`<div><div style="background: ${allColorArr[key]}"></div>${value}</div>`;
            })
            document.getElementById('cardMsgColor').innerHTML=coloeHtml;
        },
        fail: function (err, status) {
            console.log(err);
            model(1,'msg',err.msg);
        }
    })
}
cardMoney();

//各平台优惠券兑换情况分析
var amountArr=[];                   //兑换金额
var personArr=[];                   //兑换次数
var moneySum='';                   //总金额
var timesSum='';                   //总次数
var couponDistributed=function(){
    $.ajax({  
        url: serviceUrl+'/coupon-bi/bi/platformAnalyze',
        type: 'get',
        dataType: 'json',
        headers:{"Content-Type":"text/plain;charset=UTF-8"}, 
        success: function (data, status) {
            console.log('/**** 各平台优惠券兑换情况分析 ****/');
            console.log(data);
            if(data.code!=0){
                model(1,'msg',data.msg);
                return
            }
            for(var i=0;i<data.data.amount.length;i++){
                var obj={value:'',name:''};
                obj.value=data.data.amount[i].totalAmount;
                obj.name=data.data.amount[i].deduction;
                amountArr.push(obj);
            }
            for(var i=0;i<data.data.person.length;i++){
                var obj={value:'',name:''};
                obj.value=data.data.person[i].totalCustomer;
                obj.name=data.data.person[i].deduction;
                personArr.push(obj);
            }
            // console.log(amountArr);
            // console.log(personArr);
            moneySum=data.data.totalAmount;
            timesSum=data.data.totalPerson;
            peioption3.series[0].data=amountArr;
            document.getElementById('textSum').innerHTML=parseFloat(moneySum).toLocaleString();
            myChartpie3.setOption(peioption3);
        },
        fail: function (err, status) {
            console.log(err);
            model(1,'msg',err.msg);
        }
    })
}
couponDistributed();
//各平台优惠券兑换
$('.dhcoupon-tab>div').click(function(){
    var index=$(this).index();
    var html="（单位：元）" ;
    $('.dhcoupon-tab>div').removeClass('active');
    $(this).addClass('active');
    if(index==0){
        peioption3.series[0].data=amountArr;
        document.getElementById('textSum').innerHTML=parseFloat(moneySum).toLocaleString();
    }else{
        html="（单位：次）"
        peioption3.series[0].data=personArr;
        document.getElementById('textSum').innerHTML=parseFloat(timesSum).toLocaleString();
    }
    document.getElementById('canvasHeadText').innerHTML=html;
    myChartpie3.setOption(peioption3);
})

//优惠券兑换频次人数分布情况分析
var couponExchange=function(){
    $.ajax({  
        url: serviceUrl+'/coupon-bi/bi/frequencyAnalyze',
        type: 'get',
        dataType: 'json',
        headers:{"Content-Type":"text/plain;charset=UTF-8"}, 
        success: function (data, status) {
            console.log('/**** 优惠券兑换频次人数分布情况分析 ****/');
            console.log(data);
            if(data.code!=0){
                model(1,'msg',data.msg);
                return
            }
            var tiemsPeople=[];
            var tiemsArr=[];
            for(var i=0;i<data.data.exchange.length;i++){
                tiemsPeople.push(data.data.exchange[i].totalCustomer);
                tiemsArr.push(data.data.exchange[i].deductionCount);
            }
            zhuoption4.xAxis[0].data=tiemsArr;
            zhuoption4.series[0].data=tiemsPeople;
            myChartzhu4.setOption(zhuoption4);
        },
        fail: function (err, status) {
            console.log(err);
            model(1,'msg',err.msg);
        }
    })
}
couponExchange();

//优惠券兑换次数商品排行榜
var couponRanking=function(){
    $.ajax({  
        url: serviceUrl+'/coupon-bi/bi/skuAnalyze',
        type: 'get',
        dataType: 'json',
        headers:{"Content-Type":"text/plain;charset=UTF-8"}, 
        success: function (data, status) {
            console.log('/**** 优惠券兑换次数商品排行榜 ****/');
            console.log(data);
            if(data.code!=0){
                model(1,'msg',data.msg);
                return
            }
            var arr=data.data.top10;
            var ranking=[];
            var RankingHtml=`
                <div>
                    <div>排行</div>
                    <div>商品名称</div>
                    <div>兑换次数</div>
                </div>
            `;
            $.each(arr,function(key,value){
                // console.log(value);
                RankingHtml+=`
                    <div>
                        <div>${value.rowNum}</div>
                        <div>
                            ${value.deduction}--${value.skuName}
                        </div>
                        <div>${parseFloat(value.skuCount).toLocaleString()}</div>
                    </div>
                `;
            })
            document.getElementById('productRanking').innerHTML=RankingHtml;
        },
        fail: function (err, status) {
            console.log(err);
            model(1,'msg',err.msg);
        }
    })
}
couponRanking();
